<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import useStore from '@/store';
import { props } from './form';
defineOptions({ name: 'CurrencyForm' });
const _props = defineProps(props);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      window.$api.currency.save(form).then(res => {
        ElMessage.success('保存成功!');
        useStore.window.windowRemove(_props.window?.name);
        if (_props.window?.callBack) _props.window?.callBack();
      });
    }
  });
};
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  id: null,
  code: null,
  name: null,
  shortName: '',
  address: null,
  priority: null,
  areaCode: '',
  tradeCode: '',
  postCode: null,
  taxpayer: null,
  bank: null,
  bankAccount: null,
  email: null,
  fax: null,
  contactPerson: null,
});
const rules = reactive<FormRules>({
  code: [
    { required: true, message: '请输入供应商代码', trigger: 'blur' },
    { max: 50, message: '不能多于10个字符', trigger: 'blur' },
  ],
  name: [
    { required: true, message: '请输入供应商名称', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
  address: [
    { required: true, message: '请输入地址', trigger: 'blur' },
    { max: 50, message: '不能多于100个字符', trigger: 'blur' },
  ],
  priority: [
    {
      required: true,
      message: '请输入优先级',
      trigger: 'blur',
    },
  ],
});
const detail = () => {
  window.$api.currency.detail(_props.window.params.id).then(res => {
    form.id = res.data.id;
    form.code = res.data.code;
    form.name = res.data.name;
    form.shortName = res.data.shortName;
    form.address = res.data.address;
    form.priority = res.data.priority;
    form.areaCode = res.data.areaCode;
    form.tradeCode = res.data.tradeCode;
    form.postCode = res.data.postCode;
    form.taxpayer = res.data.taxpayer;
    form.bank = res.data.bank;
    form.bankAccount = res.data.bankAccount;
    form.email = res.data.email;
    form.fax = res.data.fax;
    form.contactPerson = res.data.contactPerson;
  });
};
onMounted(() => {
  if (_props.window.params) {
    if (_props.window.params.id) {
      detail();
    }
  }
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save"></windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="100px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商代码" prop="code">
            <el-input v-model="form.code" placeholder="请输入供应商代码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="priority">
            <el-input-number v-model="form.priority" :min="1" controls-position="right" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="供应商名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入供应商代码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商简称" prop="shortName">
            <el-input v-model="form.shortName" placeholder="请输入供应商代码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入地址" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="地区代码" prop="areaCode">
            <el-input v-model="form.areaCode" placeholder="请输入地区代码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易代码" prop="tradeCode">
            <el-input v-model="form.tradeCode" placeholder="请输入交易代码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮政编码" prop="postCode">
            <el-input v-model="form.postCode" placeholder="请输入邮政编码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="纳税人" prop="taxpayer">
            <el-input v-model="form.taxpayer" placeholder="请输入纳税人" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="银行" prop="bank">
            <el-input v-model="form.bank" placeholder="请输入银行" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="银行账户" prop="bankAccount">
            <el-input v-model="form.bankAccount" placeholder="请输入银行账户" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传真" prop="fax">
            <el-input v-model="form.fax" placeholder="请输入传真" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="负责人" prop="contactPerson">
            <el-input v-model="form.contactPerson" placeholder="请输入负责人" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
